package com.example.football.controller;

import com.example.football.pojo.People;
import com.example.football.pojo.Result;
import com.example.football.service.PeopleService;
import com.example.football.service.PeopleServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller

public class PeopleController {
    @Resource
    private PeopleService cc;

    //全查
    @RequestMapping("/peoplelist")
    public String queryAll(Model model) {
        List<People> list = cc.queryAll();
        //存入model
        model.addAttribute("list", list);
        //控制跳转

        return "peoplelist";
    }

    @RequestMapping("removeByPeopleid")
    public String removeByPeopleid(Integer id) {
        try {
            cc.removeByPeopleid(id);
            return "redirect:/peoplelist";
        } catch (Exception e) {
            return "error";
        }
    }

    @PostMapping("findByAccount")
    public String findByAccount(String account, Model model){
        List<People> list = cc.queryByAccount(account);
        //存入model
        model.addAttribute("list", list);
        return "peoplelist";
    }



    @RequestMapping("addPeoplePage")
    public String addPeoplePage() {
        return "addpeople";
    }

    @RequestMapping("addPeople")
    public String addPeople(People people) {
        try {
            cc.addPeople(people);
            return "redirect:/peoplelist";
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }



    @GetMapping("findById")
    public String findById(Integer id, Model model){
        People list = cc.queryById(id);
        //存入model
        model.addAttribute("list", list);
        return "updatepeople";
    }

    //数据修改
    @PostMapping("updatePeople")
    public String changePeople(People people) {
        try {
            cc.changePeople(people);
            return "redirect:/peoplelist";
        } catch (Exception e) {
            return "error";
        }
    }


}
